// pages/home/tourismFarm/tourismFarm.js
var app = getApp;
const router = require('../../../config').router

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastPage:0,
    pageNo:1,
    currentTab: 0,
    navbar:[
      {
        cate_name:'农庄景点',
        img: '../../../image/spot.png',
        img_active:'../../../image/spot_active.png',
        icon:'icon-jingdian'
      },{
        cate_name:'推荐路线',
        img: '../../../image/location.png',
        img_active: '../../../image/location_active.png',
        icon:'icon-luxian'
      }
    ],
    banners: [
      // {
      //   id: '1026',
      //   title: '福利：阳春三月乡村游，南京这些地方值得一去',
      //   image: 'http://www.jsxcpd.com/ftpdata/userfile/survey/imges/20180301/20180301160626245.jpg',
      //   description: '南京市农委特向社会推介一批三月节庆特色创意休闲活动，引导广大市民体验南京休闲农业的发展成果。现向社会发布休闲农业精品景点16个，特色活动五十余项。',
      //   createDateStr: "2018-03-01 16:07:08",
      //   video: ''
      // }
    ],
    list: [
      // {
      //   id:'784',
      //   title: '冬季到江宁来看鱼',
      //   image: 'http://www.jsxcpd.com/ftpdata/userfile/survey/imges/20180125/20180125143731517.png',
      //   description: '在南京江宁的青龙大道有一个名叫鑫淼龙锦园的地方，很适合在节假日的时候去游玩。',
      //   createDateStr: '2018-01-19 14:46:50',
      //   video:'http://www.jsxcpd.com/ftpdata/userfile/survery/video/20180125/20180125143743802.mp4'
      // }
    ],
    select: false,
    tihuoWay: '南京市',
    areaId:'',
    keyword:'',
    voteTitle:'',
    areaList:[
      // {
      //   "id": 44,
      //   "name": "南京市"
      // }
    ],
    windowHeight: '',
    windowWidth: '',
    height: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var areaId = that.data.areaId;
    var currentTab = that.data.currentTab;
    var method = '';
    //农家游城市列表
    wx.request({
      url: router,
      data: {
        method: 'query.farmTour.area',
        appKey: 'w4q897jgvxkb',
        v: '1.0',
        format: 'json',
        areaId:areaId
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data.areaList)
          that.setData({
            areaList: res.data.areaList
          })
        }
      }
    });
    //农家游轮播图
    wx.request({
      url: router,
      data: {
        method: 'query.farmTour.activity',
        appKey: 'w4q897jgvxkb',
        v: '1.0',
        format: 'json',
        areaId: areaId
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data)
          that.setData({
            banners: res.data.article
          })
        }
      }
    });
    if (currentTab == 0) {
      //农庄景点
      method = 'query.farmTour.spots'
    } else {
      //推荐路线
      method = 'query.farmTour.tour'
    }
    wx.request({
      url: router,
      data: {
        method: method,
        appKey: 'w4q897jgvxkb',
        v: '1.0',
        format: 'json',
        areaId: areaId,
        keyword:'',
        pageNo:1
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data)
          that.setData({
            list: res.data.article,
            lastPage: res.data.page.last,
            pageNo: res.data.page.pageNo,
          })
        }
      }
    });
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },
  imageError(e) {
    var that = this
    var height = e.detail.height;
    var width = e.detail.width;
    var ratio = width / height
    var height1 = parseInt(that.data.windowWidth / ratio);
    console.log(height1)
    that.setData({
      height: height1 + 'px'
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  //显示城市列表
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  //根据城市渲染数据
  mySelect(e) {
    var that = this;
    var name = e.currentTarget.dataset.name;
    var id = e.currentTarget.dataset.id;
    that.setData({
      tihuoWay: name,
      areaId: id,
      select: false
    })
    this.onLoad();
  },
  close(){
    var that = this;
    that.setData({
      select: false
    })
  },
  //tab切换
  navbarTap: function (e) {
    var that = this;
    var currentTab = e.currentTarget.dataset.idx;
    var areaId = that.data.areaId;
    var method = '';
    // console.log(currentTab)
    that.setData({
      currentTab: currentTab,
      keyword:''
    })
    if (currentTab == 0){
      //农庄景点
      method = 'query.farmTour.spots'
    }else{
      //推荐路线
      method = 'query.farmTour.tour'
    }
    wx.request({
      url: router,
      data: {
        method: method,
        appKey: 'w4q897jgvxkb',
        v: '1.0',
        format: 'json',
        areaId: areaId,
        keyword: ''
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data)
          that.setData({
            list: res.data.article
          })
        }
      }
    });
  },
  //searchClick搜索
  searchClick: function (e) {
    var that = this;
    var keyword = that.data.keyword;
    var areaId = that.data.areaId;
    var currentTab = that.data.currentTab;
    var method = '';
    if (keyword == ''){
      wx.showModal({
        title: "提示",
        content: "请输入关键字！",
        showCancel: false,
        confirmText: "确定"
      })
    }else{
      if (currentTab == 0) {
        //农庄景点
        method = 'query.farmTour.spots'
      } else {
        //推荐路线
        method = 'query.farmTour.tour'
      }
      wx.request({
        url: router,
        data: {
          method: method,
          appKey: 'w4q897jgvxkb',
          v: '1.0',
          format: 'json',
          areaId: areaId,
          keyword: keyword
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            if (res.data.article){
              that.setData({
                list: res.data.article
              })
            }else{
              wx.showModal({
                title: "提示",
                content: "暂无搜索结果！",
                showCancel: false,
                confirmText: "确定"
              })
            }
          }
        }
      });
    }
  },
  //获取input值
  voteTitle: function (e) {
    var that = this;
    var value = e.detail.value;
    // console.log(e.detail.value);
    that.setData({
      keyword: value
    })
  },

  /**
   * 详情
   */
  articleClick: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/index?id=' + id
    })
  },
  //上拉
  onReachBottom() {
    var that = this;
    var method = ''
    var currentTab = that.data.currentTab;//判断
    if (currentTab == 0) {
      //农庄景点
      var last = that.data.lastPage
      var p = this.data.pageNo
      p++
      method = 'query.farmTour.spots'
    } else {
      //推荐路线
      var last = that.data.lastPage
      var p = this.data.pageNo
      p++
      method = 'query.farmTour.tour'
    }
    
    if (p <= last) {
      wx.request({
        url: router,
        data: {
          appKey: 'w4q897jgvxkb',
          method: method,
          v: '1.0',
          format: 'json',
          pageNo: p
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            // console.log(res.data)
            for (var i = 0; i < res.data.article.length; i++) {
              that.data.list.push(res.data.article[i])
            }
            wx.showToast({
              title: '加载中',
              icon: 'loading',
              duration: 1000
            })
            setTimeout(function () {
              that.setData({
                list: that.data.list,
                pageNo: res.data.page.pageNo,
                lastPage: res.data.page.lastPage
              })
            }, 1000)
          }
        }
      });
    }
  }
})